package com.molichuxing.gateway.bff.dealer.convertor;

import java.time.LocalTime;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.gateway.bff.dealer.entity.request.modify.create.StoreCreateVo;
import com.molichuxing.gateway.bff.dealer.entity.response.StoreInfoVo;
import com.molichuxing.gateway.bff.dealer.entity.response.StoreModifyVo;
import com.molichuxing.gateway.property.StoreStatusAndVerifyEnum;
import com.molichuxing.services.business.dto.request.create.StockBizCreateDto;
import com.molichuxing.services.business.dto.request.create.StoreBizCreateDto;
import com.molichuxing.services.business.dto.request.modify.StockBizModifyDto;
import com.molichuxing.services.business.dto.request.modify.StoreBizModifyDto;
import com.molichuxing.services.business.dto.response.StoreBizDto;
import com.molichuxing.services.business.dto.response.StoreInfoBizDto;
import com.molichuxing.services.infrastructure.dto.response.RegionInfoDto;
import com.molichuxing.services.property.StoreStatusEnum;
import com.molichuxing.services.property.StoreVerifyStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;

import com.molichuxing.gateway.bff.dealer.entity.response.StoreVo;
import com.molichuxing.gateway.utils.entity.DealerEntity;
import com.molichuxing.services.infrastructure.dto.response.DealerDto;
import com.molichuxing.services.infrastructure.dto.response.StoreDto;
import com.molichuxing.services.property.DealerStatusEnum;
import com.molichuxing.services.property.DealerTypeEnum;

public class Convert {

	public static DealerEntity toDealerEntity(DealerDto beanDto) {
		DealerEntity entity = null;
		if (beanDto != null) {
			entity = new DealerEntity();
			BeanUtils.copyProperties(beanDto, entity);
			DealerStatusEnum statusEnum = null;
			if ((statusEnum = DealerStatusEnum.getEnum(beanDto.getStatus())) != null) {
				entity.setStatus(statusEnum.getValue());
			}
			entity.setId(beanDto.getDealerId());

			DealerTypeEnum type = null;
			if ((type = DealerTypeEnum.getEnum(beanDto.getType())) != null) {
				entity.setType(type.getValue());
				entity.setTypeStr(type.getName());
			}

		}
		return entity;
	}

	public static List<StoreVo> toStoreVo(List<StoreDto> dtoList) {
		List<StoreVo> voList = null;
		if (dtoList != null && !dtoList.isEmpty()) {
			voList = new ArrayList<>();
			for (StoreDto beanDto : dtoList) {
				voList.add(toStoreVo(beanDto));
			}
		}
		return voList;
	}

	public static StoreVo toStoreVo(StoreDto beanDto) {
		StoreVo beanVo = null;
		if (beanDto != null) {
			beanVo = new StoreVo();
			BeanUtils.copyProperties(beanDto, beanVo);
		}

		return beanVo;
	}


	/*********************************门店转换开始*************************************************/

	public static List<StoreVo> toStoreVoList(List<StoreBizDto> bizDtoList, Map<Integer, RegionInfoDto> regionInfoMap) {
		List<StoreVo> voList = new ArrayList<>();
		if (bizDtoList != null && bizDtoList.size() > 0) {
			for (StoreBizDto bizDto : bizDtoList) {
				voList.add(toStoreVo(bizDto, regionInfoMap));
			}
		}
		return voList;
	}

	public static StoreVo toStoreVo(StoreBizDto bizDto, Map<Integer, RegionInfoDto> regionInfoMap) {
		StoreVo storeVo = new StoreVo();
		if (null == bizDto) {
			return storeVo;
		}

		BeanUtils.copyProperties(bizDto, storeVo);
		Integer regionId = bizDto.getRegionId();
		if (regionInfoMap != null && regionInfoMap.size() > 0) {
			RegionInfoDto regionInfoDto = regionInfoMap.get(regionId);
			if (regionInfoDto != null) {
				String provinceName = regionInfoDto.getProvinceFullName();
				String cityName = regionInfoDto.getCityFullName();
				Integer provinceId = regionInfoDto.getProvinceId();

				storeVo.setProvinceName(provinceName);
				storeVo.setCityName(cityName);
				storeVo.setProvinceId(provinceId);
			}
		}

		Integer verifyStatus = bizDto.getVerifyStatus();
		Integer status = bizDto.getStatus();
		String storeStatusStr = getStoreStatusStr(verifyStatus, status);
		storeVo.setStoreStatusStr(storeStatusStr);
		return storeVo;
	}


	private static String getStoreStatusStr(Integer verifyStatus, Integer status) {
		if (StoreVerifyStatusEnum.UN_VERIFY.getValue() == verifyStatus) {
			return StoreStatusAndVerifyEnum.UN_VERIFY.getName();
		} else if (StoreVerifyStatusEnum.VERIFY_REJECT.getValue() == verifyStatus) {
			return StoreStatusAndVerifyEnum.VERIFY_REJECT.getName();
		} else {
			if (StoreStatusEnum.OPEN.getValue() == status) {
				return StoreStatusAndVerifyEnum.OPEN.getName();
			} else if (StoreStatusEnum.SUSPENSION.getValue() == status) {
				return StoreStatusAndVerifyEnum.SUSPENSION.getName();
			}
		}
		return "";
	}

	public static StoreBizModifyDto toStoreBizModifyDto(StoreModifyVo storeModifyVo) {
		StoreBizModifyDto storeBizModifyDto = new StoreBizModifyDto();
		if (storeModifyVo != null) {
			BeanUtils.copyProperties(storeModifyVo, storeBizModifyDto);

			String businessStartTime = storeModifyVo.getBusinessStartTime();
			String busineesEndTime = storeModifyVo.getBusineesEndTime();
			if (StringUtils.isNoneBlank(businessStartTime)) {
				LocalTime startTime = DateUtil.parseTime(businessStartTime);
				storeBizModifyDto.setBusinessStartTime(startTime);
			}
			if (StringUtils.isNoneBlank(busineesEndTime)) {
				LocalTime endTime = DateUtil.parseTime(busineesEndTime);
				storeBizModifyDto.setBusineesEndTime(endTime);
			}
		}
		return storeBizModifyDto;
	}

	public static StockBizModifyDto toStockBizModifyDto(StoreModifyVo storeModifyVo) {
		StockBizModifyDto stockBizModifyDto = new StockBizModifyDto();
		if (storeModifyVo != null) {
			BeanUtils.copyProperties(storeModifyVo, stockBizModifyDto);
		}
		return stockBizModifyDto;
	}

	public static StoreBizCreateDto toStoreBizCreateDto(StoreCreateVo storeCreateVo) {
		StoreBizCreateDto storeBizCreateDto = new StoreBizCreateDto();
		if (storeCreateVo != null) {
			BeanUtils.copyProperties(storeCreateVo, storeBizCreateDto);
			String businessStartTime = storeCreateVo.getBusinessStartTime();
			String busineesEndTime = storeCreateVo.getBusineesEndTime();
			if (StringUtils.isNoneBlank(businessStartTime)) {
				LocalTime startTime = DateUtil.parseTime(businessStartTime);
				storeBizCreateDto.setBusinessStartTime(startTime);
			}
			if (StringUtils.isNoneBlank(busineesEndTime)) {
				LocalTime endTime = DateUtil.parseTime(busineesEndTime);
				storeBizCreateDto.setBusineesEndTime(endTime);
			}
		}
		return storeBizCreateDto;
	}

	public static StockBizCreateDto toStockBizCreateDto(StoreCreateVo storeCreateVo) {
		StockBizCreateDto stockBizCreateDto = new StockBizCreateDto();
		if (storeCreateVo != null) {
			BeanUtils.copyProperties(storeCreateVo, stockBizCreateDto);
		}
		return stockBizCreateDto;
	}

	public static StoreInfoVo toStoreInfoVo(StoreInfoBizDto storeInfoBizDto) {
		StoreInfoVo storeInfoVo = new StoreInfoVo();
		if (storeInfoBizDto != null) {
			BeanUtils.copyProperties(storeInfoBizDto, storeInfoVo);
			Integer verifyStatus = storeInfoBizDto.getVerifyStatus();
			Integer status = storeInfoBizDto.getStatus();
			String storeStatusStr = getStoreStatusStr(verifyStatus, status);
			storeInfoVo.setStoreStatusStr(storeStatusStr);
		}
		return storeInfoVo;
	}
	/*********************************门店转换结束***********************************************/
}
